package net.xdclass.controller;

import io.swagger.v3.oas.annotations.Operation;
import net.xdclass.annotation.ShareCodeCheck;
import net.xdclass.aspect.ShareCodeAspect;
import net.xdclass.controller.req.*;
import net.xdclass.dto.AccountFileDTO;
import net.xdclass.dto.ShareDTO;
import net.xdclass.dto.ShareDetailDTO;
import net.xdclass.dto.ShareSimpleDTO;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.service.ShareService;
import net.xdclass.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author 姜阿木
 * @date 2025/7/22 15:18
 */
@RestController
@RequestMapping("/api/share/v1")
public class ShareController {

    @Autowired
    private ShareService shareService;


    /**
     * 获取我的个人分享文件列表
     * @param
     * @return
     */
    @GetMapping("list")
    @Operation(summary = "获取我的个人分享文件列表", description = "获取我的个人分享文件列表")
    public JsonData list(){
        List<ShareDTO> list =shareService.listShare();
        return JsonData.buildSuccess(list);
    }

    /**
     * 创建分享
     * @param
     * @return
     */
    @PostMapping("create")
    @Operation(summary = "创建分享", description = "创建分享")
    public JsonData create(@RequestBody ShareCreateReq Req){
        Req.setAccountId(LoginInterceptor.threadLocal.get().getId());
        ShareDTO shareDTO =shareService.createShare(Req);
        return JsonData.buildSuccess(shareDTO);
    }

    /**
     * 取消分享
     */
    @PostMapping("cancel")
    @Operation(summary = "取消分享", description = "取消分享")
    public JsonData cancel(@RequestBody ShareCancelReq req){
        req.setAccountId(LoginInterceptor.threadLocal.get().getId());
        shareService.cancelShare(req);
        return JsonData.buildSuccess();
    }

    /**
     * 访问分享接口，返回基本分享信息
     * 1、情况一:不需要提取码
     * 2、情况二:需要提取码
     */
    @GetMapping("visit")
    @Operation(summary = "访问分享接口", description = "访问分享接口")
    public JsonData visit(@RequestParam(value = "shareId") Long shareId){
        ShareSimpleDTO shareSimpleDTO = shareService.simpleDetail(shareId);
        return JsonData.buildSuccess(shareSimpleDTO);
    }

    /**
     * 校验分享码
     */
    @PostMapping("check_share_code")
    @Operation(summary = "校验分享码", description = "校验分享码")
    public JsonData checkShareCode(@RequestBody ShareCheckReq req){
        String shareToken = shareService.checkShareCode(req);
        if (shareToken == null){
            return JsonData.buildResult(BizCodeEnum.SHARE_NOT_EXIST);
        }
        return JsonData.buildSuccess(shareToken);
    }

    /**
     * 查看分享详情接口
     */
    @GetMapping("detail")
    @ShareCodeCheck
    @Operation(summary = "查看分享详情接口", description = "查看分享详情接口")
    public JsonData detail(){
        Long shareId = ShareCodeAspect.getShareId();
        ShareDetailDTO shareDetailDTO =shareService.detail(shareId);
        return JsonData.buildSuccess(shareDetailDTO);

    }

    /**
     * 查看分享文件夹下的文件列表
     */
    @PostMapping("list_share_file")
    @ShareCodeCheck
    @Operation(summary = "查看分享文件夹下的文件列表", description = "查看分享文件夹下的文件列表")
    public JsonData listShareFile(@RequestBody ShareFileQueryReq req){
        req.setShareId(ShareCodeAspect.getShareId());
        List<AccountFileDTO> list = shareService.listShareFile(req);
        return JsonData.buildSuccess(list);
    }

    /**
     * 文件分享转存
     */
    @PostMapping("transfer")
    @ShareCodeCheck
    @Operation(summary = "文件分享转存", description = "文件分享转存")
    public JsonData transfer(@RequestBody ShareFileTransferReq req){
        req.setShareId(ShareCodeAspect.getShareId());
        req.setAccountId(LoginInterceptor.threadLocal.get().getId());
        shareService.transferShareFile(req);
        return JsonData.buildSuccess();
    }
}









